//
//  TZWNewFeatureController.swift
//  Sinaweibotzw
//
//  Created by 檀志文 on 16/4/12.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class TZWNewFeatureController: UICollectionViewController {

    // 布局参数
    let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
    
    init() {
        
        super.init(collectionViewLayout: layout)
        // 设置layout 参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()

        self.collectionView!.registerClass(TZWNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // MARK: UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TZWNewFeatureCell
    
        cell.index = indexPath.item
        
        return cell
    }

//    在一个文件中写多个类
    class TZWNewFeatureCell: UICollectionViewCell {
        
        
        var index: Int = 0 {
            didSet {
//            当控制器给cell设置值后
//                拼接图片名称
                let imageName = "new_feature_\(index + 1)"
                HMPrint("imageName: \(imageName)")
                bkgImageView.image = UIImage(named: imageName)
                //           隐藏按钮
                startButton.hidden = true
            }

        }
        
        // 系统会调用这个方法
        override init(frame: CGRect){
            super.init(frame: frame)
            prepareUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        private func prepareUI() {
//            1 添加控件
             // 当是cell的时候,view都添加到contentView
            
            contentView.addSubview(bkgImageView)
            contentView.addSubview(startButton)
//            2 添加约束
            bkgImageView.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(UIEdgeInsetsZero)
                
//            开始按钮
           startButton.snp_makeConstraints(closure: { (make) -> Void in
                make.centerX.equalTo(contentView)
                make.bottom.equalTo(contentView).offset(-160)
           })
            }
        }
        
//        MARK: 动画方法
        func startAnimation(){
//            显示按钮
            startButton.hidden = false
//            将transform 的缩放改成为0
            startButton.transform = CGAffineTransformMakeScale(0, 0)
            
            UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    HMPrint("动画完成了")
            }
        }
        
        // MARK: 按钮的点击事件
        @objc private func didClickStartButton(){
            let controller = TZWMainTableViewController()
            (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
        }
        
        //   MARK: 懒加载
        //    背景
        private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
        
        /**
        *  开始按钮
        */
        private lazy var startButton: UIButton = {
            let button = UIButton()
            /**
            *  设置
            */
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
           
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
            
            button.setTitle("开始体验", forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(NewFeatureLabelFontSize)
            
            button.addTarget(self, action: "didClickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
            
            button.sizeToFit()
            return button
        }()
        
    }
    
    
}
// MARK: - 扩展HMNewFeatureViewController 实现 scrollViewDidEndDecelerating 方法
extension TZWNewFeatureController {
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView){
        let index = Int(scrollView.contentOffset.x/UIScreen.mainScreen().bounds.width)
        //  判断是否是最后一页
        if index == NumberOfNewFeatureItem - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            //            最后一页,让cell开始动画
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! TZWNewFeatureCell
            cell.startAnimation()
        }
    }
}
